#include <stdio.h>
#include "stm32f10x.h"
#include "bsp.h"
#include "lora.h"
#include "rs485.h"
#include "bsp_flash.h"


extern u32 Valid_Data_Len_Max;
u32 	tick_led = 0;
u32   tick_Config_RS485 = 0;
u32 	tick_lora = 0;
u32 	tick_RS485_tx_user = 0;
u32 RS485_TxData_Period;
u8 RS485_TxData_Flag=0;
char RS485_TxData_user[256];
char Lora_Recv_to_485[100];
u16 RS485_TxData_length_user=0;
char* pcRS485_Rx = NULL;
char* ultrLoraRx = NULL;

u32 	ulRS485_Size;
u32   ulLoraRxSize;


char RS485_RcvData_Buff[8][101];



extern u32 Rs485RxFlag;
extern u32  gLoRaRxFlag;

u16 RS485_Baud_Config[3];
u32 RS485_Baud_Config_BaseAddr=0x08019000;
u16 RS485_Baud_Config_Read[3];
char RS485_Baud_Config_Read_User[2];

u16 RS485_Send_Data[128];
u32 RS485_Send_Data_BaseAddr=0x08019400;
u16 RS485_Send_Data_Read[128];

char RS485_Baud_StopBit=0x01; 
	
char User_Config_Begin[6]={0x42,0x45,0x47,0x49,0x4e,0x0a};
char User_Config_End[4]={0x45,0x4e,0x44,0x0a};
char User_Config_Erase[6]={0x45,0x52,0x41,0x53,0x45,0X0a};
char User_Config_Baud[5]={0x42,0x41,0x55,0x44,0x0a};
char User_Config_Data[5]={0x44,0x41,0x54,0x41,0x0a};

/*
*********************************************************************************************************
*	函 数 名: CRC16
*	功能说明: 计算CRC-16值
*	返 回 值: CRC结果
*   作    者：黄铭
*   日    期：2019-02-18
*********************************************************************************************************/
u16 CRC16( u8 *buff, u8 len)
{
	u16 crc=0xFFFF;
	u8 i, j, Data;
	for ( j=0; j<len; j++ )
	{
		crc=crc ^*buff++;
		for ( i=0; i<8; i++)
		{
			if( (crc&0x0001) >0)

			{
				crc=crc>>1;
				crc=crc^0xa001;
			}
			else
				crc=crc>>1;
		}
	}
	return (crc);
}

/*
*********************************************************************************************************
*	函 数 名: Read_RS485_Baud_Config
*	功能说明: 读取flash中的波特率配置部分的信息，如果没有写波特率，默认9600
*	返 回 值: 返回波特率
*   作    者：黄铭
*   日    期：2019-03-07
*********************************************************************************************************/
u32 Read_RS485_Baud_Config()
{
	u32 RS485_baud;
	STMFLASH_Read(RS485_Baud_Config_BaseAddr,RS485_Baud_Config_Read,3);
	if(RS485_Baud_Config_Read[0]==0x0202)
	{
		if(RS485_Baud_Config_Read[1]==0x0001)  {RS485_baud=1200; RS485_Baud_Config_Read_User[0]=0x01;}
		else if(RS485_Baud_Config_Read[1]==0x0002) {RS485_baud=2400; RS485_Baud_Config_Read_User[0]=0x02;}
		else if(RS485_Baud_Config_Read[1]==0x0003) {RS485_baud=4800; RS485_Baud_Config_Read_User[0]=0x03;}
		else if(RS485_Baud_Config_Read[1]==0x0004) {RS485_baud=9600; RS485_Baud_Config_Read_User[0]=0x04;}
		else if(RS485_Baud_Config_Read[1]==0x0005) {RS485_baud=14400; RS485_Baud_Config_Read_User[0]=0x05;}
		else if(RS485_Baud_Config_Read[1]==0x0006) {RS485_baud=19200; RS485_Baud_Config_Read_User[0]=0x06;}
		else if(RS485_Baud_Config_Read[1]==0x0007) {RS485_baud=38400; RS485_Baud_Config_Read_User[0]=0x07;}
		else if(RS485_Baud_Config_Read[1]==0x0008) {RS485_baud=56000; RS485_Baud_Config_Read_User[0]=0x08;}
		else if(RS485_Baud_Config_Read[1]==0x0009) {RS485_baud=57600; RS485_Baud_Config_Read_User[0]=0x09;}
		else if(RS485_Baud_Config_Read[1]==0x000A) {RS485_baud=115200; RS485_Baud_Config_Read_User[0]=0x0A;}
		
		if(RS485_Baud_Config_Read[2]==0x0001) {RS485_Baud_StopBit=0x01; RS485_Baud_Config_Read_User[1]=0x01;}
			else if(RS485_Baud_Config_Read[2]==0x0002) {RS485_Baud_StopBit=0x02; RS485_Baud_Config_Read_User[1]=0x02;}
			else if(RS485_Baud_Config_Read[2]==0x0003) {RS485_Baud_StopBit=0x03; RS485_Baud_Config_Read_User[1]=0x03;}
	}
	else
	{
		RS485_baud=9600;
		RS485_Baud_Config_Read_User[0]=0x04;
		RS485_Baud_Config_Read_User[1]=0x01;
	}
	return RS485_baud;
		
}



/*
*********************************************************************************************************
*	函 数 名: Read_RS485_User_Config
*	功能说明: 读取flash中的波特率与周期性发送数据的配置部分的信息，如果没有写波特率，默认9600，如果没有配置周期
*           性发送的数据，则不发送
*	返 回 值: 无
*   作    者：黄铭
*   日    期：2019-03-07
*********************************************************************************************************/
void Read_RS485_User_Config()
{
	u16 p;
	u16 n;
	STMFLASH_Read(RS485_Send_Data_BaseAddr,RS485_Send_Data_Read,2);
	
	if(RS485_Send_Data_Read[0]==0x0202)
	{
		n=RS485_Send_Data_Read[1];
		if(n%2==0)
			{
				STMFLASH_Read(RS485_Send_Data_BaseAddr,RS485_Send_Data_Read,(4+n/2));
				
				RS485_TxData_Period=((u32)RS485_Send_Data_Read[3])*1000;
				RS485_TxData_Flag=1;
				RS485_TxData_length_user=n;
				for(p=0;p<n/2;p++)
				{
					RS485_TxData_user[2*p+1]=RS485_Send_Data_Read[p+4];
					RS485_TxData_user[2*p]=RS485_Send_Data_Read[p+4]>>8;
				}

			}
		else
			{
				STMFLASH_Read(RS485_Send_Data_BaseAddr,RS485_Send_Data_Read,(4+(n+1)/2));
				
				RS485_TxData_Period=((u32)RS485_Send_Data_Read[3])*1000;
				RS485_TxData_Flag=1;
				RS485_TxData_length_user=n;
				for(p=0;p<(n-1)/2;p++)
				{
					RS485_TxData_user[2*p+1]=RS485_Send_Data_Read[p+4];
					RS485_TxData_user[2*p]=RS485_Send_Data_Read[p+4]>>8;
				}
				RS485_TxData_user[2*p]=RS485_Send_Data_Read[p+4]>>8;
			}
		
	}
else
	{
		
	}
	
}

/*
*********************************************************************************************************
*	函 数 名: User_Config_Baud_RS485
*	功能说明: 用户配置波特率以及周期性发送数据
*	返 回 值: 无
*   作    者：黄铭
*   日    期：2019-03-07
*********************************************************************************************************/
void User_Config_Baud_RS485()
{
	u8 state = 1;
	u32 m = 0;
	u16 n;
	u32 baud;
	if(state==1)
	{
		while(1)
		{
		if(!tick_Config_RS485) tick_Config_RS485 = millis();
			if((millis() - tick_Config_RS485) <= 10000)
			{
				if(bspUsart2Available())	
					{
							ulRS485_Size = Rs485Receive((void*)&pcRS485_Rx);
							if( (ulRS485_Size==6) && (pcRS485_Rx[0]=='#') && (pcRS485_Rx[1]=='#') && (pcRS485_Rx[2]=='B') && (pcRS485_Rx[3]=='E')&& (pcRS485_Rx[4]=='#')&& (pcRS485_Rx[5]=='#') )
								{
										state = 2;
										Rs485Send(User_Config_Begin,6);
										bspUsart2CleanData();
										tick_Config_RS485 = 0;
										break;
								}			
					}			
			}
			else
			{
				tick_Config_RS485 = 0;
				break;
			}
		}
	}
	
	
	
	if(state==2)
	{
		while(1)
		{
		
			
		
				if(bspUsart2Available())	
					{
							ulRS485_Size = Rs485Receive((void*)&pcRS485_Rx);
							m=ulRS485_Size;
							if( (m==10) && (pcRS485_Rx[0]=='#') && (pcRS485_Rx[1]=='#') && (pcRS485_Rx[2]==0x42) && (pcRS485_Rx[3]==0x55) && (pcRS485_Rx[4]==0x00) && (pcRS485_Rx[6]==0x00) && (pcRS485_Rx[8]=='#') && (pcRS485_Rx[9]=='#') )
							{
								if((pcRS485_Rx[5]==0x01) || (pcRS485_Rx[5]==0x02) || (pcRS485_Rx[5]==0x03) || (pcRS485_Rx[5]==0x04) || (pcRS485_Rx[5]==0x05) || (pcRS485_Rx[5]==0x06) || (pcRS485_Rx[5]==0x07) || (pcRS485_Rx[5]==0x08) || (pcRS485_Rx[5]==0x09) || (pcRS485_Rx[5]==0x0A) ) 
									{ 
										if( (pcRS485_Rx[7]==0x01) ||(pcRS485_Rx[7]==0x02) ||(pcRS485_Rx[7]==0x03))
										{
											RS485_Baud_Config[0]=0x0202;
											RS485_Baud_Config[1]=((u16)pcRS485_Rx[5]);
											RS485_Baud_Config[2]=((u16)pcRS485_Rx[7]);
											STMFLASH_Write(RS485_Baud_Config_BaseAddr,RS485_Baud_Config,3);
											delay(50);
											Rs485Send(User_Config_Baud,5);
											bspUsart2CleanData();
										}
									}		
							}	
							else if( (m==6) && (pcRS485_Rx[0]=='#') && (pcRS485_Rx[1]=='#') && (pcRS485_Rx[2]=='E') && (pcRS485_Rx[3]=='D')&& (pcRS485_Rx[4]=='#')&& (pcRS485_Rx[5]=='#') )	
							{	
								Rs485Send(User_Config_End,4);
								bspUsart2CleanData();
								break;
							}	
							else if( (pcRS485_Rx[0]=='#') && (pcRS485_Rx[1]=='#') && (pcRS485_Rx[2]==0x01) && (pcRS485_Rx[3]==0x01) && (pcRS485_Rx[4]==0x00) && (pcRS485_Rx[m-2]=='#')&& (pcRS485_Rx[m-1]=='#') )	
							{
								if(pcRS485_Rx[5]%2==0)		
								{
									RS485_Send_Data[0]=0x0202;
									RS485_Send_Data[1]=((u16)pcRS485_Rx[4])<<8 | pcRS485_Rx[5]; 
									RS485_Send_Data[2]=((u16)pcRS485_Rx[6])<<8 | pcRS485_Rx[7]; 
									RS485_Send_Data[3]=((u16)pcRS485_Rx[8])<<8 | pcRS485_Rx[9]; 
									for(n=0;n<(pcRS485_Rx[5]/2);n++)
									{
										RS485_Send_Data[4+n]=((u16)pcRS485_Rx[10+2*n])<<8 | pcRS485_Rx[11+2*n];
									}
									STMFLASH_Write(RS485_Send_Data_BaseAddr,RS485_Send_Data,4+n);
									
								}
								else
								{
									RS485_Send_Data[0]=0x0202;
									RS485_Send_Data[1]=((u16)pcRS485_Rx[4])<<8 | pcRS485_Rx[5]; 
									RS485_Send_Data[2]=((u16)pcRS485_Rx[6])<<8 | pcRS485_Rx[7]; 
									RS485_Send_Data[3]=((u16)pcRS485_Rx[8])<<8 | pcRS485_Rx[9]; 
									for(n=0;n<((pcRS485_Rx[5]-1)/2);n++)
									{
										RS485_Send_Data[4+n]=((u16)pcRS485_Rx[10+2*n])<<8 | pcRS485_Rx[11+2*n];
									}
									RS485_Send_Data[4+n]=((u16)pcRS485_Rx[10+2*n])<<8;
									STMFLASH_Write(RS485_Send_Data_BaseAddr,RS485_Send_Data,5+n);
									
								}
								Rs485Send(User_Config_Data,5);
								bspUsart2CleanData();
							}
							else if((m==6) && (pcRS485_Rx[0]=='#') && (pcRS485_Rx[1]=='#') && (pcRS485_Rx[2]=='F') && (pcRS485_Rx[3]=='F')&& (pcRS485_Rx[4]=='#')&& (pcRS485_Rx[5]=='#'))	
							{
								for(n=0;n<128;n++)
									{
										RS485_Send_Data[n]=0x0000;
									}
									STMFLASH_Write(RS485_Send_Data_BaseAddr,RS485_Send_Data,128);
									delay(200);
									for(n=0;n<3;n++)
									{
										RS485_Baud_Config[n]=0x0000;
									}
									STMFLASH_Write(RS485_Baud_Config_BaseAddr,RS485_Baud_Config,3);
									delay(100);
									Rs485Send(User_Config_Erase,6);
									RS485_TxData_Flag=0;
									RS485_TxData_length_user=0;
									
							}
							else if((m==7) && (pcRS485_Rx[0]=='#') && (pcRS485_Rx[1]=='#') && (pcRS485_Rx[2]==0x3F) && (pcRS485_Rx[3]==0x42) && (pcRS485_Rx[4]==0x45) && (pcRS485_Rx[5]=='#')&& (pcRS485_Rx[6]=='#'))
							{
								baud=Read_RS485_Baud_Config();
								Rs485Send(RS485_Baud_Config_Read_User,2);
								bspUsart2CleanData();
							}
							else if((m==7) && (pcRS485_Rx[0]=='#') && (pcRS485_Rx[1]=='#') && (pcRS485_Rx[2]==0x3F) && (pcRS485_Rx[3]==0x44) && (pcRS485_Rx[4]==0x41) && (pcRS485_Rx[5]=='#')&& (pcRS485_Rx[6]=='#'))
							{
								Read_RS485_User_Config();
								if(RS485_TxData_Flag==1)
									{
										Rs485Send(RS485_TxData_user,RS485_TxData_length_user);
										bspUsart2CleanData();
									}

							}
					}	
					
			
			
			
			
			
		}
	}
	
}



int main(void)
{
 	
	u32 timeout = 0;
	u32 RS485_baud;
	u8 i = 0;
	u8 k=0;
	u8 state = 1;
	u32 j = 0;
	u8 m = 0;
	u8 n;
	u8 Rs485_Valid_len=0;
	u16 CRC_Data;
	u8 RS485_RcvData_Buff_index=0;
	u8 Lora_TxData_Buff_index=0;
	for(i=0;i<8;i++)
		RS485_RcvData_Buff[i][0]=0; 
	bspInit();
	/*
	读取485波特率的配置信息，如果未配置或清除了所有配置，则按照默认9600，如果配置了则按照配置的波特率去初始化485
	*/
	
	RS485_baud=Read_RS485_Baud_Config();
	
	Rs485Init(RS485_baud);
	delay(100);
	bspUsart2CleanData();
	
	User_Config_Baud_RS485();

	Read_RS485_User_Config();
	
	RS485_baud=Read_RS485_Baud_Config();
	Rs485Init(RS485_baud);
	
	LoRaInit();
	delay(100);
	
	
	bspUsart3Send("AT+CLASS?\r\n",10);
	delay(1000);
	bspUsart3CleanData();
	
	bspUsart3Send("AT+CLASS=01\r\n",12);
	delay(1000);
	bspUsart3CleanData();
	
	
	if(RS485_TxData_Flag==1)
		{
			Rs485Send(RS485_TxData_user,RS485_TxData_length_user); 
		}
			
	while(1)
	{
		
		if(!tick_led) tick_led = millis();
		if((millis() - tick_led) >= 100)
		{
			tick_led = 0;
			bspLedToggle(LED1);
		}
	
	
		if(!tick_lora) tick_lora = millis();
		if((millis() - tick_lora) >= 600000) 
		{
			if(Check_Lora_Join_Status()==0)
				Set_Lora_Join();
			tick_lora = 0;
		}
	
		if(!tick_RS485_tx_user) tick_RS485_tx_user = millis();
		if((millis() - tick_RS485_tx_user) >= RS485_TxData_Period) 
		{
			if(RS485_TxData_Flag==1)
			{
				Rs485Send(RS485_TxData_user,RS485_TxData_length_user);
				tick_RS485_tx_user = 0;
			}
		}
	
			
			if(bspUsart2Available())	
				{

					ulRS485_Size = Rs485Receive((void*)&pcRS485_Rx);
					RS485_RcvData_Buff_index++;
					RS485_RcvData_Buff_index = RS485_RcvData_Buff_index & 0x07;
					
					if(ulRS485_Size<100)
						Rs485_Valid_len = ulRS485_Size;
					else
						Rs485_Valid_len = 100;
					
					
					RS485_RcvData_Buff[RS485_RcvData_Buff_index][0] = Rs485_Valid_len;					
					for(j=1;j<(Rs485_Valid_len+1);j++)
					{
						RS485_RcvData_Buff[RS485_RcvData_Buff_index][j] = pcRS485_Rx[j-1];
					}
				}

			
			i=Lora_TxData_Buff_index;
			while(i<(8+Lora_TxData_Buff_index))
			{
				
				k=i&0x07;
				i++;
				if(RS485_RcvData_Buff[k][0]>0)
				{
					if(Check_Lora_Join_Status())
					{
						Lora_Tx_Data(RS485_RcvData_Buff[k], (RS485_RcvData_Buff[k][0])+1);
						Lora_TxData_Buff_index=i&0x07;
						RS485_RcvData_Buff[k][0]=0;
						break;
					}
					else
					{
						if(Set_Lora_Join())
						{
							Lora_Tx_Data(RS485_RcvData_Buff[k], (RS485_RcvData_Buff[k][0])+1);
							
							Lora_TxData_Buff_index=i&0x07;
							RS485_RcvData_Buff[k][0]=0;
							break;
						}
						else
							break;
					}					
					break;
				}
			}


		
			
			if(bspUsart3Available())	
				{

					ulLoraRxSize =  bspUsart3Receive((void*)&ultrLoraRx);
					if(ulLoraRxSize>19) 
					{
						if( (ultrLoraRx[0]=='O') && (ultrLoraRx[1]=='K') && (ultrLoraRx[2]=='+') && (ultrLoraRx[3]=='R') && (ultrLoraRx[4]=='E') && (ultrLoraRx[5]=='C') && (ultrLoraRx[6]=='V') && (ultrLoraRx[7]==':') && (ultrLoraRx[10]==',') && (ultrLoraRx[13]==',') && (ultrLoraRx[16]==',') && (ultrLoraRx[ulLoraRxSize-2]==0X0D) && (ultrLoraRx[ulLoraRxSize-1]==0x0A) )
						{
							n=0;
							for(m=17;m<ulLoraRxSize-2;m++,m++)
							{
								Lora_Recv_to_485[n]=asciitohex(ultrLoraRx[m],ultrLoraRx[m+1]);
								n++;
							}
							if( (n==1) && (Lora_Recv_to_485[0]==0x00) )
							;
								else
							Rs485Send(Lora_Recv_to_485,n);
							
						}
						
						
					}
					bspUsart3CleanData();
				}
					
					
					
			
			
	}
	
	return 0;
}
